/* 
 * CMS Editor Javascript routines
 * 
 */

var xics_edit_menu_timeout = null;

$(document).ready(function () {
    
    processXicsButtons();
});

function processXicsButtons () {
    $('.xicsbuttons').each(function (index, elem) {
        var inner = $(elem).find('.xicsbuttonsinner');
        $(document.body).append(inner);
        $($(elem).find('.xicseditmenu img')).click(function () {
            if ($(inner).css('display') != 'none') {
                $(inner).hide('fast');
            } else {
                $('.xicsbuttonsinner').css('display', 'none');
                var offset = $(elem).offset();
                $(inner).css('left', offset.left)
                    .css('top', offset.top + 16)
                    .show('fast')
                    .mouseleave(function () {
                        $(inner).hide('fast');
                    });
            }
        });
        
    });    
}

/**
 * 
 * @param url  Url to send request to
 * @param id   Id to replace with response
 * @param data Request parameters (optional)
 */
function xicsEditorRequest (url, id) {
    $('.xicsbuttonsinner').css('display', 'none');
    $('#'+id).mask('Laden ...');
    var req = {
      type: 'POST',
      url: url,
      success: function (data) {
          $('#'+id).html(data);
          processXicsButtons();
          $('#'+id).unmask();
      },
      dataType: 'html'
    };
    if (arguments[2]) {
        req.data = arguments[2];
    }
    $.ajax(req);
}

function xicsScrollToView (id) {
  var scrolltop = $(window).scrollTop();
  var offset    = $('#'+id).offset();
  if (offset == null) return;
  var vpheight  = $(window).height();
  // if not in viewport
  if (arguments[1] || offset.top < scrolltop || offset.top + 100 > scrolltop+vpheight-$('#'+id).height()/2) {
      $(window).scrollTo('#'+id, 1000, {offset: -100, easing: 'easeOutQuart'})
//      $('html,body').animate({scrollTop: (offset.top-100)}, 500, 'easeOutQuart');
 }
}